<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Html5相关Dog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src='https://gitee.com/mingyueyixi/H5-Dog/widget_preview'></script>
    <style>
        .pro_name a {
            color: #4183c4;
        }

        .osc_git_title {
            background-color: #fff;
        }

        .osc_git_box {
            background-color: #fff;
        }

        .osc_git_box {
            border-color: #E3E9ED;
        }

        .osc_git_info {
            color: #666;
        }

        .osc_git_main a {
            color: #9B9B9B;
        }

        .content {
            margin: 20px auto;
        }

        ul, li, ol {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        body, html {
            height: 100%;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: #F0F0F1;
            margin: 0 auto;
            padding: 0 10px;

        }

        .li-top {
            color: #ffffff;
            font-size: 18px;
            background-color: #56AA7E;
            text-align: center;
            padding: 10px 0;
        }

        .li-content {
            padding: 10px;
            display: none;
        }

        .li-content.open{
            display: block;
            animation: open-el 0.3s;
        }
        .li-content.close{
            animation: close-el 0.3s;
        }

        @keyframes open-el {
            from{
                opacity: 0;
            }
            to{
                opacity: 1;
            }
        }
        @keyframes close {
            from{
                display: block;
                opacity: 1;
            }
            to{
                opacity: 0;
                display: none;
            }
        }
        .li-content a {
            padding: 5px;
            display: block;
            color: #444444;
            background: none;
            text-decoration: none;
        }

        .li-content a:hover {
            background-color: #D2EBE9;
        }

        li:after {
            box-sizing: border-box;
            display: block;
            content: '';
            height: 1px;
            width: 100%;
            background-color: #F1F1F1;
        }


        @media (min-width: 360px) {
            body{
                width: 100%;
            }
        }

        @media (min-width: 960px) {
            body{
                width: 800px;
            }
        }
        .ui.segment.osc_git_box{
            width: 100%;
        }


    </style>

</head>

<body>


<div class="content">

    <ul id="ulFiles">

    </ul>

</div>
<script>
    var files = <%- JSON.stringify(files) %>;
    var ulFiles = document.getElementById("ulFiles");

    var barList = [];

    function hasBar(val) {
        for (var j = 0; j < barList.length; j++) {
            if (barList[j] === val)
                return true;
        }
    }

    function initHtml() {

        for (var i = 0; i < files.length; i++) {

            files[i] = files[i].replace(/[\\]/g, "/");
            var text = files[i].substring("public/src/".length, files[i].length);

            var end = text.indexOf("/");

            var title = text.substring(0, end);
            if (end === -1) {
                title = 'src';
            }

            var barId = 'li-' + title;

            var elA = document.createElement("a");
            elA.innerText = files[i].substring("public/src/".length + end + 1, files[i].length);
            elA.href = files[i].substring("public".length, files[i].length);
            elA.target = "_blank";

            if (!hasBar(title)) {
                barList.push(title);

                var li = document.createElement("li");
                li.id = barId;

                var liTop = document.createElement("div");
                liTop.innerText = title;
                liTop.className = 'li-top';

                var liContent = document.createElement("div");

                liContent.appendChild(elA);
                liContent.className = 'li-content';

                li.append(liTop);
                li.appendChild(liContent)

                ulFiles.appendChild(li);
            } else {
                var li = document.getElementById(barId);
                var liContent = li.childNodes[1];
                liContent.appendChild(elA);
            }

        }
    }

    function listens() {

        var elLis = document.getElementsByTagName("li");
        for (var j = 0; j < elLis.length; j++) {
            let li = elLis[j];
            let elTop = li.getElementsByTagName("div")[0];//
            let elContent = li.getElementsByTagName('div')[1];//
            //或者新建函数，传i
            //或者对象绑定elTop.i = i，this.i再拿;
            elTop.onclick = function (e) {
                // if (elContent.style.display === "" || elContent.style.display === 'none') {
                //     elContent.style.display = 'block';
                // } else {
                //     elContent.style.display = 'none';
                // }
                console.log(elContent.className)
                if (elContent.className==="li-content close" || elContent.className === "li-content"){
                    elContent.className="li-content open";
                } else{
                    elContent.className="li-content close";
                }
            }


        }
    }

    initHtml();
    listens();

</script>
</body>

</html>